﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace CRMSalesManagement.Manage.FrontPageManagement
{
    public partial class ProductAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.lblName.Text = "*";
            //this.lblMotto.Text = "*";
            //this.lblIntro.Text = "*";
            //this.lblSaveResute.Text = "";
            //this.lblPutIn.Text = "";
        }

        protected void btnPutIn_Click(object sender, EventArgs e)
        {
            //this.RequiredFieldValidator1.Enabled = false;
            string savePath = "";
            if (this.fulBrowser.HasFile)
            {
                string fileExt;
                fileExt = System.IO.Path.GetExtension(fulBrowser.FileName);
                if (fileExt == ".jpg" || fileExt == ".jpeg" || fileExt == ".png" || fileExt == ".gif")
                {
                    //  string fileContentType = fulBrowser.PostedFile.ContentType; //获取图片类型
                    savePath = Server.MapPath("~/images/ProductsPhotos/");
                    if (!Directory.Exists(savePath))
                    {
                        this.lblPutIn.Text = "找不到路径";
                    }
                    else
                    {
                        savePath += fulBrowser.FileName;
                        HttpPostedFile upFile = this.fulBrowser.PostedFile;
                        //获取上传文件大小，以字节为单位
                        int fileLength = upFile.ContentLength;
                        if (fileLength > 150 * 1024)
                        {
                            this.lblPutIn.Text = "图片大小不能超过150K";
                        }
                        else
                        {
                            fulBrowser.SaveAs(savePath);
                            this.imgProduct.ImageUrl = ("~/images/ProductsPhotos/") + this.fulBrowser.FileName;
                            this.lblPutIn.Text = "上传成功";
                        }
                    }
                }
                else
                {
                    this.lblPutIn.Text = "图片格式错误：只允许上传jpg、jpeg、png、gif文件";
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtExternalDiameter.Value == "" || txtHeight.Value == "" || txtInnerDiameter.Value == "" || txtLimitSpeed.Value == "" || txtModel.Value == "" || txtReferenceSpeed.Value == "")
                {
                    lblSaveResute.Text = "带*的内容不能为空";
                }
                else
                {
                    if (txtExternalDiameter.Value == "" || txtHeight.Value == "" || txtInnerDiameter.Value == "" || txtLimitSpeed.Value == "" || txtModel.Value == "" || txtReferenceSpeed.Value == "")
                    {
                        lblSaveResute.Text = "带*的内容不能为空";
                    }
                    else
                    {
                        if (Convert.ToInt32(this.txtInnerDiameter.Value.Trim()) >= Convert.ToInt32(this.txtExternalDiameter.Value.Trim()))
                        {
                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('轴承参数错误：内径不能大于或等于外径！');</script>");
                            // Response.Write("<script>alert('轴承参数错误：内径不能大于或等于外径！')</script>");
                        }
                        else
                        {
                            if (Convert.ToInt32(this.txtReferenceSpeed.Value.Trim()) >= Convert.ToInt32(this.txtLimitSpeed.Value.Trim()))
                            {
                                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('轴承参数错误：参考转速不能大于或等于极限转速！');</script>");
                                //Response.Write("<script>alert('轴承参数错误：参考转速不能大于或等于极限转速！')</script>");
                            }
                            else
                            {
                                CRM.DataLayer.Product products = new CRM.DataLayer.Product();
                                products.model = this.txtModel.Value.Trim();
                                products.type = this.ddlType.Text;
                                products.brand = this.ddlBrand.Text;
                                products.innerDiameter =Convert.ToInt32(this.txtInnerDiameter.Value.Trim());
                                products.externalDiameter = Convert.ToInt32(this.txtExternalDiameter.Value.Trim());
                                products.height = Convert.ToInt32(this.txtHeight.Value.Trim());
                                products.limitSpeed = Convert.ToInt32(this.txtLimitSpeed.Value.Trim());
                                products.referenceSpeed =Convert.ToInt32(this.txtReferenceSpeed.Value.Trim());
                                products.image = this.imgProduct.ImageUrl;
                                bool addProduct = CRM.Business.Product.AddProduct(products);
                                if (addProduct)
                                {
                                    ClientScript.RegisterStartupScript(this.GetType(), "", "window.alert('保存成功'); location.href='ProductsList.aspx' ", true);

                                }
                                else
                                {
                                    this.lblSaveResute.Text = "保存失败";
                                    //this.txtIntro.Text = "";
                                    //this.txtMotto.Text = "";
                                    //this.txtName.Value = "";
                                }
                            }
                        }
                    }
                }
            }catch(Exception)
            {                
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('保存失败，请检查输入是否合法……');</script>");
            }
        }
    }
}